home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / global / printUtils.js < prev    next >
Encoding:
JavaScript  |  2005-10-19  |  9.6 KB  |  266 lines

  1. //@line 41 "/c/mozilla/toolkit/components/printing/content/printUtils.js"
  2.  
  3. var  XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  4.  
  5. var gPrintSettingsAreGlobal = false;
  6. var gSavePrintSettings = false;
  7.  
  8. var PrintUtils = {
  9.  
  10.   showPageSetup: function ()
  11.   {
  12.     try {
  13.       var printSettings = this.getPrintSettings();
  14.       var PRINTPROMPTSVC = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
  15.                                      .getService(Components.interfaces.nsIPrintingPromptService);
  16.       PRINTPROMPTSVC.showPageSetup(window, printSettings, null);
  17.       if (gSavePrintSettings) {
  18.         // Page Setup data is a "native" setting on the Mac
  19.         var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
  20.                               .getService(Components.interfaces.nsIPrintSettingsService);
  21.         PSSVC.savePrintSettingsToPrefs(printSettings, true, printSettings.kInitSaveNativeData);
  22.       }
  23.     } catch (e) {
  24.       dump("showPageSetup "+e+"\n");
  25.       return false;
  26.     }
  27.     return true;
  28.   },
  29.  
  30.   print: function ()
  31.   {
  32.     var webBrowserPrint = this.getWebBrowserPrint();
  33.     var printSettings = this.getPrintSettings();
  34.     try {
  35.       webBrowserPrint.print(printSettings, null);
  36.       if (gPrintSettingsAreGlobal && gSavePrintSettings) {
  37.         var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
  38.                               .getService(Components.interfaces.nsIPrintSettingsService);
  39.         PSSVC.savePrintSettingsToPrefs(printSettings, true,
  40.                                        printSettings.kInitSaveAll);
  41.         PSSVC.savePrintSettingsToPrefs(printSettings, false,
  42.                                        printSettings.kInitSavePrinterName);
  43.       }
  44.     } catch (e) {
  45.       // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
  46.       // causing an exception to be thrown which we catch here.
  47.       // Unfortunately this will also consume helpful failures, so add a
  48.       // dump("print: "+e+"\n"); // if you need to debug
  49.     }
  50.   },
  51.  
  52.   printPreview: function (aEnterPPCallback, aExitPPCallback)
  53.   {
  54.     // if we're already in PP mode, don't set the callbacks; chances
  55.     // are they're null because someone is calling printPreview() to
  56.     // get us to refresh the display.
  57.     var pptoolbar = document.getElementById("print-preview-toolbar");
  58.     if (!pptoolbar) {
  59.       this._onEnterPP = aEnterPPCallback;
  60.       this._onExitPP  = aExitPPCallback;
  61.     } else {
  62.       // hide the toolbar here -- it will be shown in
  63.       // onEnterPrintPreview; this forces a reflow which fixes display
  64.       // issues in bug 267422.
  65.       pptoolbar.hidden = true;
  66.     }
  67.  
  68.     this._webProgressPP = {};
  69.     var ppParams        = {};
  70.     var notifyOnOpen    = {};
  71.     var webBrowserPrint = this.getWebBrowserPrint();
  72.     var printSettings   = this.getPrintSettings();
  73.     // Here we get the PrintingPromptService so we can display the PP Progress from script
  74.     // For the browser implemented via XUL with the PP toolbar we cannot let it be
  75.     // automatically opened from the print engine because the XUL scrollbars in the PP window
  76.     // will layout before the content window and a crash will occur.
  77.     // Doing it all from script, means it lays out before hand and we can let printing do it's own thing
  78.     var PPROMPTSVC = Components.classes["@mozilla.org/embedcomp/printingprompt-service;1"]
  79.                                .getService(Components.interfaces.nsIPrintingPromptService);
  80.     // just in case we are already printing, 
  81.     // an error code could be returned if the Prgress Dialog is already displayed
  82.     try {
  83.       PPROMPTSVC.showProgress(this, webBrowserPrint, printSettings, this._obsPP, false,
  84.                               this._webProgressPP, ppParams, notifyOnOpen);
  85.       if (ppParams.value) {
  86.         var webNav = getBrowser().webNavigation;
  87.         ppParams.value.docTitle = webNav.document.title;
  88.         ppParams.value.docURL   = webNav.currentURI.spec;
  89.       }
  90.  
  91.       // this tells us whether we should continue on with PP or 
  92.       // wait for the callback via the observer
  93.       if (!notifyOnOpen.value.valueOf() || this._webProgressPP.value == null)
  94.         this.enterPrintPreview();
  95.     } catch (e) {
  96.       this.enterPrintPreview();
  97.     }
  98.   },
  99.  
  100.   getWebBrowserPrint: function ()
  101.   {
  102.     return _content.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  103.                    .getInterface(Components.interfaces.nsIWebBrowserPrint);
  104.   },
  105.  
  106.   ////////////////////////////////////////
  107.   // "private" methods. Don't use them. //
  108.   ////////////////////////////////////////
  109.  
  110.   setPrinterDefaultsForSelectedPrinter: function (aPSSVC, aPrintSettings)
  111.   {
  112.     if (!aPrintSettings.printerName)
  113.       aPrintSettings.printerName = aPSSVC.defaultPrinterName;
  114.  
  115.     // First get any defaults from the printer 
  116.     aPSSVC.initPrintSettingsFromPrinter(aPrintSettings.printerName, aPrintSettings);
  117.     // now augment them with any values from last time
  118.     aPSSVC.initPrintSettingsFromPrefs(aPrintSettings, true,  aPrintSettings.kInitSaveAll);
  119.   },
  120.  
  121.   getPrintSettings: function ()
  122.   {
  123.     var pref = Components.classes["@mozilla.org/preferences-service;1"]
  124.                          .getService(Components.interfaces.nsIPrefBranch);
  125.     if (pref) {
  126.       gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
  127.       gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
  128.     }
  129.  
  130.     var printSettings;
  131.     try {
  132.       var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
  133.                             .getService(Components.interfaces.nsIPrintSettingsService);
  134.       if (gPrintSettingsAreGlobal) {
  135.         printSettings = PSSVC.globalPrintSettings;
  136.         this.setPrinterDefaultsForSelectedPrinter(PSSVC, printSettings);
  137.       } else {
  138.         printSettings = PSSVC.newPrintSettings;
  139.       }
  140.     } catch (e) {
  141.       dump("getPrintSettings: "+e+"\n");
  142.     }
  143.     return printSettings;
  144.   },
  145.  
  146.   _chromeState: {},
  147.   _closeHandlerPP: null,
  148.   _webProgressPP: null,
  149.   _onEnterPP: null,
  150.   _onExitPP: null,
  151.  
  152.   // This observer is called once the progress dialog has been "opened"
  153.   _obsPP: 
  154.   {
  155.     observe: function(aSubject, aTopic, aData)
  156.     {
  157.       // delay the print preview to show the content of the progress dialog
  158.       setTimeout(function () { PrintUtils.enterPrintPreview(); }, 0);
  159.     },
  160.  
  161.     QueryInterface : function(iid)
  162.     {
  163.       if (iid.equals(Components.interfaces.nsIObserver) || iid.equals(Components.interfaces.nsISupportsWeakReference))
  164.         return this;   
  165.       throw Components.results.NS_NOINTERFACE;
  166.     }
  167.   },
  168.  
  169.   enterPrintPreview: function ()
  170.   {
  171.     var webBrowserPrint = this.getWebBrowserPrint();
  172.     var printSettings   = this.getPrintSettings();
  173.     try {
  174.       webBrowserPrint.printPreview(printSettings, null, this._webProgressPP.value);
  175.     } catch (e) {
  176.       // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
  177.       // causing an exception to be thrown which we catch here.
  178.       // Unfortunately this will also consume helpful failures, so add a
  179.       // dump(e); // if you need to debug
  180.       return;
  181.     }
  182.  
  183.     var printPreviewTB = document.getElementById("print-preview-toolbar");
  184.     if (printPreviewTB) {
  185.       printPreviewTB.updateToolbar();
  186.       printPreviewTB.hidden = false;
  187.       return;
  188.     }
  189.  
  190.     // show the toolbar after we go into print preview mode so
  191.     // that we can initialize the toolbar with total num pages
  192.     printPreviewTB = document.createElementNS(XUL_NS, "toolbar");
  193.     printPreviewTB.setAttribute("printpreview", true);
  194.     printPreviewTB.setAttribute("id", "print-preview-toolbar");
  195.  
  196. //@line 244 "/c/mozilla/toolkit/components/printing/content/printUtils.js"
  197.  
  198.     // copy the window close handler
  199.     if (document.documentElement.hasAttribute("onclose"))
  200.       this._closeHandlerPP = document.documentElement.getAttribute("onclose");
  201.     else
  202.       this._closeHandlerPP = null;
  203.     document.documentElement.setAttribute("onclose", "PrintUtils.exitPrintPreview(); return false;");
  204.  
  205.     // disable chrome shortcuts...
  206.     window.addEventListener("keypress", this.onKeyPressPP, true);
  207.  
  208.     _content.focus();
  209.  
  210.     // on Enter PP Call back
  211.     if (this._onEnterPP) {
  212.       this._onEnterPP();
  213.       this._onEnterPP = null;
  214.     }
  215.   },
  216.  
  217.   exitPrintPreview: function ()
  218.   {
  219.     window.removeEventListener("keypress", this.onKeyPressPP, true);
  220.  
  221. //@line 272 "/c/mozilla/toolkit/components/printing/content/printUtils.js"
  222.  
  223.     // restore the old close handler
  224.     document.documentElement.setAttribute("onclose", this._closeHandlerPP);
  225.     this._closeHandlerPP = null;
  226.  
  227.     if ("getStripVisibility" in getBrowser())
  228.       getBrowser().setStripVisibilityTo(this._chromeState.hadTabStrip);
  229.  
  230.     var webBrowserPrint = this.getWebBrowserPrint();
  231.     webBrowserPrint.exitPrintPreview(); 
  232.  
  233.     // remove the print preview toolbar
  234.     var printPreviewTB = document.getElementById("print-preview-toolbar");
  235.     getBrowser().parentNode.removeChild(printPreviewTB);
  236.  
  237.     _content.focus();
  238.  
  239.     // on Exit PP Call back
  240.     if (this._onExitPP) {
  241.       this._onExitPP();
  242.       this._onExitPP = null;
  243.     }
  244.   },
  245.  
  246.   onKeyPressPP: function (aEvent)
  247.   {
  248.     var closeKey;
  249.     try {
  250.       closeKey = document.getElementById("key_close")
  251.                          .getAttribute("key");
  252.       closeKey = aEvent["DOM_VK_"+closeKey];
  253.     } catch (e) {}
  254.     var isModif = aEvent.ctrlKey || aEvent.metaKey;
  255.     // ESC and Ctrl-W exits the PP
  256.     if (aEvent.keyCode == aEvent.DOM_VK_ESCAPE || isModif &&
  257.         (aEvent.charCode == closeKey || aEvent.charCode == closeKey + 32))
  258.       PrintUtils.exitPrintPreview();
  259.     // cancel shortkeys
  260.     if (isModif) {
  261.       aEvent.preventDefault();
  262.       aEvent.stopPropagation();
  263.     }
  264.   }
  265. }
  266.